<?php


namespace RedMVC\Event;
use RedMVC;

/**
 * Dispatcher class
 *
 * @category    RedMVC
 * @package     Event
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Dispatcher{

    const EVENT_DISPATCH_PRE            = 'onPreDispatch';
    const EVENT_DISPATCH_POST           = 'onPostDispatch';    
    const EVENT_CONTROLLER_NOT_EXISTS   = 'onControllerNotExists';
    const EVENT_ACTION_NOT_EXISTS       = 'onActionNotExists';
    const EVENT_ROUTER_INIT             = 'onRouterInit';
    const EVENT_FORM_RENDER             = 'onFormRender';
    const EVENT_FORM_CALLBACK           = 'onFormCallback';

    protected static $_callbacks = array();

    public static function register($event, $callback){
        self::$_callbacks[$event][] = $callback;
    }

    public static function fire($event, $params = array()){
        $return = null;
        if(isset(self::$_callbacks[$event])){
            $return = array();
            foreach(self::$_callbacks[$event] as $callback){
                $callbackReturn = call_user_func_array($callback, $params);
                if(null !== $callbackReturn){
                    $return[] = $callbackReturn;
                }
            }
        }
        return (count($return) > 0) ? $return : null;
    }

}